package com.g4.shoppingback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.common.TextVo;
import com.g4.shoppingback.pojo.DdProductInfo;
import com.g4.shoppingback.mapper.DdProductInfoMapper;
import com.g4.shoppingback.service.IDdProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingback.service.ImagesVoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@Service
public class DdProductInfoServiceImpl extends ServiceImpl<DdProductInfoMapper, DdProductInfo> implements IDdProductInfoService {

    @Autowired
    private DdProductInfoMapper ddProductInfoMapper;



    @Autowired
    private ImagesVoService imagesVoService;



    //商品表
    //商品类别页面添加
    @Override
    public void addPro(DdProductInfo ddProductInfo) {

        //入库添加商品页面信息
        ddProductInfoMapper.insert(ddProductInfo);
    }

    //商品删除下架
    @Override
    public void deletePro(Integer Id) {

        //根据id查询Product表的数据,
        DdProductInfo msg = ddProductInfoMapper.selectById(Id);
        //获取图片路径集合
        String[] images = msg.getPdImage().split(",");
        //遍历,并删除指定图片
        for (String imagePath : images){
            imagesVoService.deleteImages(imagePath);
        }

        ddProductInfoMapper.deleteAll(Id);
    }


    //根据Id对商品进行状态的修改
    @Override
    public void updateById(Integer id, Integer saleState) {

        ddProductInfoMapper.setSta(id,saleState);
    }




    //根据新网页中传入数据的ID,更新数据库数据
    @Override
    public void updateWeb(DdProductInfo ddProductInfo) {

        ddProductInfoMapper.updateById(ddProductInfo);
    }



    //todo:无法通过类型查找
    @Override
    public PageVo selectByName(PageVo pageVo) {

        Integer pageSize = 5;   //设置每页固定显示5条数据
        Integer pageStart = (pageVo.getPageNum()-1)*pageSize;   //设置从第几条开始

        //查询总条数
        Long total = ddProductInfoMapper.selectTotal(pageVo.getQuery());


        //根据用户输入信息进行查询数据 集合
        List<TextVo> querylist= ddProductInfoMapper.selectByQuery(pageVo.getQuery(),pageStart,pageSize);


        //封装信息
        pageVo.setPageSize(pageSize).setTotal(total).setRows(querylist);
        return pageVo;
    }


    //根据id查询商品
    @Override
    public DdProductInfo selectById(Integer id) {
        DdProductInfo ddProductInfo = ddProductInfoMapper.selectById(id);
        return ddProductInfo;
    }
}



